package postpc.ex1.ListLayoutsSelector;

import postpc.ex1.ListLayoutsSelector.Layouts.GridLayoutActivity;
import postpc.ex1.ListLayoutsSelector.Layouts.LinearLayoutActivity;
import postpc.ex1.ListLayoutsSelector.Layouts.ListLayoutActivity;
import postpc.ex1.ListLayoutsSelector.Layouts.RelativeLayoutActivity;
import postpc.ex1.ListLayoutsSelector.Layouts.TabLayoutActivity;
import postpc.ex1.ListLayoutsSelector.Layouts.TableLayoutActivity;
import postpc.ex1.ListLayoutsSelector.Widgets.FormStuffWidgetActivity;
import postpc.ex1.ListLayoutsSelector.Widgets.MapViewWidgetActivity;
import postpc.ex1.ListLayoutsSelector.Widgets.PickersWidgetActivity;
import postpc.ex1.ListLayoutsSelector.Widgets.WebViewWidgetActivity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListLayoutsSelector extends ListActivity {

	/** Called when the activity is first created. */

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.d("EX1", "Started List layout selector");

		// The layouts for the first activity - to select the other layouts.
		String[] LAYOUTS = getResources().getStringArray(R.array.layouts_array);
		setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, LAYOUTS));

		ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		// Listener for the click on the list of layouts.
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

				// Connects a list item to its intent.
				Intent intent = null;
				switch (position) {
				case 0:
					Log.d("EX1", "Selected 0");
					intent = new Intent(ListLayoutsSelector.this, LinearLayoutActivity.class);
					break;
				case 1:
					Log.d("EX1", "Selected 1");
					intent = new Intent(ListLayoutsSelector.this, RelativeLayoutActivity.class);
					break;
				case 2: 
					Log.d("EX1", "Selected 2");
					intent = new Intent(ListLayoutsSelector.this, TableLayoutActivity.class);
					break;
				case 3:
					Log.d("EX1", "Selected 3");
					intent = new Intent(ListLayoutsSelector.this, GridLayoutActivity.class);
					break;
				case 4:
					Log.d("EX1", "Selected 4");
					intent = new Intent(ListLayoutsSelector.this, TabLayoutActivity.class);
					break;
				case 5:
					Log.d("EX1", "Selected 5");
					intent = new Intent(ListLayoutsSelector.this, ListLayoutActivity.class);
					break;
				case 6:
					Log.d("EX1", "Selected 6");
					intent = new Intent(ListLayoutsSelector.this, PickersWidgetActivity.class);
					break;
				case 7:
					Log.d("EX1", "Selected 7");
					intent = new Intent(ListLayoutsSelector.this, FormStuffWidgetActivity.class);
					break;
				case 8:
					Log.d("EX1", "Selected 8");
					intent = new Intent(ListLayoutsSelector.this, MapViewWidgetActivity.class);
					break;
				case 9:
					Log.d("EX1", "Selected 9");
					intent = new Intent(ListLayoutsSelector.this, WebViewWidgetActivity.class);
					break;
				default:
					Log.e("EX1", "List item not connected !");
					Toast.makeText(getApplicationContext(), "Cannot get the option you choose", Toast.LENGTH_SHORT).show();
					break;
				}
				if (intent != null){
					Log.d("EX1", "Sending intent");
					startActivity(intent);
				}       
			}
		});
	}
}